package com.flyme.module.base.provider.service.impl;


import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysHistory;
import com.flyme.module.base.provider.mapper.SysHistoryMapper;
import com.flyme.module.base.provider.service.SysHistoryService;
import org.springframework.stereotype.Service;

@Service
public class SysHistoryServiceImpl extends BaseServiceImpl<SysHistoryMapper, SysHistory> implements SysHistoryService {




    /**
     * 根据用户查询某个课程的观看记录
     */
    public SysHistory findByUser(String accountInfoId, String courseId) {
        return getOne(q().eq(true, "userId", accountInfoId).eq(true, "courseId", courseId) ,false);
    }

    /**
     * 网站根据用户查询浏览记录
     */
    public PqGrid selectByUserWeb(Long accountInfoId, PagerInfo pagerInfo){
        pagerInfo.setPageNumber(4);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("h.id historyId,c.isVipFree,c.courseImage,c.coursePrice,c.courseTitle,c.id courseId,c.courseCount cataNum,l.lecturerName");
        queryWrapper.eq("h.userId", accountInfoId);
        queryWrapper.orderByDesc("h.createTime");
        queryWrapper.createAlias("course", "c");
        queryWrapper.createAlias("lecturer", "l");
        return pageList(queryWrapper);
    }

    /**
     * 网站根据用户查询浏览记录
     */
    public PqGrid selectByUserWeb2(Long accountInfoId, PagerInfo pagerInfo){
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("h.id historyId,c.courseImage,c.coursePrice,c.courseTitle,c.id courseId,l.lecturerName userName," +
            "(SELECT u.avatar FROM sys_user u WHERE u.id = c.lecturerId) avatar,c.recordNum");
        queryWrapper.eq("h.userId", accountInfoId);
        queryWrapper.orderByDesc("h.createTime");
        queryWrapper.createAlias("course", "c");
        queryWrapper.createAlias("lecturer", "l");
        return pageList(queryWrapper);
    }

    public boolean removeAll(Long userId){
        return remove(q().eq("userId", userId));
    }

}
